跳转至

文件

File

可以实现文件的重命名、创建、删除等操作,但不含读写

示例代码:

public static void main(String[] args) {
  File file1 = new File("TestFolder\\MiniTest1.java");
  System.out.println(file1.canExecute()); //bool
  System.out.println(file1.canRead()); // bool
  System.out.println(file1.exists()); // bool
  System.out.println(file1.isFile()); // bool
  System.out.println(file1.isDirectory()); // bool
  System.out.println(file1.length()); // int
  System.out.println(file1.getName()); // String

  // 除此以外,还有一些 methods
  file1.setWritable(false);
  file1.delete();
  file1.renameTo();
}

用 Scanner 配合 File 类读取文本文件

public static void main(String[] args) {
  File records = new File("TestFolder\\ScoreSheet.txt");
  try {
    Scanner recs = new Scanner(records); // 这里不是 System.in 了
    recs.nextLine();
    while (recs.hasNext()) {
      String name = recs.next();
      String sNum = recs.next();
      double score = recs.nextDouble();
      System.out.println(name + ", " + sNum + ", " + score);
    }
    recs.close();
  } catch (FileNotFoundException e) {
  }
}

用 PrintWriter 配合 File 类写入文本文件

但是这样做会清空之前的文件内容

File records = new File("TestFolder\\ScoreSheet.txt");
PrintWriter pw = new PrintWriter(records);
pw.print("DPD 0000 0.5");

要想追加内容,可以把 File 改成 FileWriter

FileWriter fw;
try {

  fw = new FileWriter("TestFolder\\ScoreSheet.txt", true);
  PrintWriter pw = new PrintWriter(fw);
  pw.print("DPD 0000 0.5");
  pw.close();
} catch (IOException e1) {
}

用 FileInputStream 读取二进制文件

import java.io.FileInputStream;
import java.io.IOException;

public class TestFileInputStream {
  public static void main(String[] args) {
    try {
      // File size in byte: 17,171,531 (~16.3MB)
      FileInputStream fin = new FileInputStream("ManyNumbers.cs161");
      long start = System.currentTimeMillis();
      byte[] b = new byte[1]; // buffer size
      while (fin.available() > 0) {
        fin.read(b);
        if (b[0] > 15)
          b[0] = 0;
      }
      fin.close();
      long end = System.currentTimeMillis();
      System.out.println((end - start) / 1000); // 104 seconds
    } catch (IOException e) {
    }
  }
}

用 BufferedInputStream 读取

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class TestBufferedInputStream {
  public static void main(String[] args) {
    try {
      // File size in byte: 17,171,531 (~16.3MB)
      BufferedInputStream fin = new BufferedInputStream(new FileInputStream("ManyNumbers.cs161"));
      long start = System.currentTimeMillis();
      byte[] b = new byte[1];
      while (fin.available() > 0) {
        fin.read(b);
        for (int i = 0; i < b.length; i++) {
          if (b[0] > 15)
            b[0] = 0;
        }
      }
      fin.close();
      long end = System.currentTimeMillis();
      System.out.println((end - start) / 1000.0); // 64.34 seconds
    } catch (IOException e) {
    }
  }
}

用 FileOutputStream 写入文件

import java.io.FileOutputStream;
import java.io.IOException;

public class TestFileOutputStream {
  public static void main(String[] args) {
    try {
      FileOutputStream fout = new FileOutputStream("NewNumbers.cs161");
      byte[] b = new byte[64];
      for (int i = 0; i < b.length; i++)
        b[i] = (byte) (i + 41);
      fout.write(b);
      // fout.close(); // Didn’t close the file, but the contents still have been written to the
      // file.
    } catch (IOException e) {
    }
  }
}

BufferedOutputStream 写入文件

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestBufferedOutputStream {
  public static void main(String[] args) {
    try {
      BufferedOutputStream fout =
          new BufferedOutputStream(new FileOutputStream("NewNumbers.cs161"));
      byte[] b = new byte[64];
      for (int i = 0; i < b.length; i++) {
        b[i] = (byte) (i + 41);
      }
      fout.write(b);
      // 下面两行至少要执行其中一个才能保证文件写入成功
      fout.flush(); // Didn’t flush
      fout.close(); // Didn’t close
    } catch (IOException e) {
    }
  }
}